home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / mahe / aheclip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-17  |  2.6 KB  |  102 lines

  1. /*
  2.  *    AHECLIP.C : Modify the histogram for a region of the image
  3.  *    in order to control the mapping function.
  4.  *
  5.  *    Corrected a bug from the integer operations
  6.  *                    --- Shie Jue Lee
  7.  *                        Nov. 13, 1987
  8.  *    Modified by Shie-Jue Lee on invalid clip checking.
  9.  *        N is changed from N = xdim * ydim to
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <math.h>
  14. /*#include <image.h>*/
  15. #include "ahe.h"
  16.  
  17. aheclip(histo, maxlevels, cliplimit, xdim, ydim, ptemp, p)
  18.      int        *histo,        /* histogram array */
  19.      maxlevels,        /* number of grey levels in image */
  20.      xdim,            /* region size in x */
  21.      ydim,            /* region size in y */
  22.      cliplimit,        /* computed limit of bucket count */
  23.      *ptemp;        /* floor of p */
  24.      float      *p;        /* base into which excess just fits */
  25. {
  26.     int        *h,        /* pointer to histogram */
  27.     num_above,            /* number of buckets at or above limit */
  28.     histsize,            /* total room in histogram */
  29.     top,            /* high value in binary search */
  30.     bottom,            /* low value in binary search */
  31.     limit,            /* cliplimit - ptemp */
  32.     excess,            /* number of pixels above cliplimit */
  33.     N;                /* region area(in pixels) */
  34.     
  35.     N = xdim * ydim ;    /* region size */
  36.     histsize = (cliplimit * maxlevels);    /* histogram size */
  37.     
  38.     if(histsize < N) {
  39.     /* not enough room */
  40.     fprintf(stderr, "invalid clip limit\n"); 
  41.     exit(-1); 
  42.     } else if(histsize == N) {
  43.     /* set all bins to cliplimit */
  44.     for(h = histo; h < histo + maxlevels; h++)
  45.         *h = cliplimit; 
  46.     *ptemp = 0; 
  47.     *p = 0.0; 
  48.     return; 
  49.     }
  50.     
  51.     
  52.     if(calcexcess(cliplimit, histo, maxlevels) == 0) {
  53.     /* no clipping needed */
  54.     *ptemp = 0; 
  55.     *p = 0.0; 
  56.     return; 
  57.     } else {
  58.     /* binary search for ptemp */
  59.     bottom = 0; 
  60.     top = cliplimit; 
  61.     excess = calcexcess(cliplimit, histo, maxlevels); 
  62.     while((top - bottom) > 1) {
  63.         *ptemp = (top + bottom) / 2.0; 
  64.         limit = cliplimit - *ptemp; 
  65.         excess = calcexcess(limit, histo, maxlevels); 
  66.         if(*ptemp * maxlevels > excess)
  67.         top = *ptemp; 
  68.         else
  69.         bottom = *ptemp; 
  70.     }
  71.     *ptemp = bottom; 
  72.     limit = cliplimit - *ptemp; 
  73.     
  74.     /* calculate number of buckets above(cliplimit-ptemp) */
  75.     /* and also clip histogram at(cliplimit-ptemp) */
  76.     num_above = 0; 
  77.     for(h = histo; h < histo + maxlevels; h++)
  78.         if(*h >= limit) {
  79.         num_above++; 
  80.         *h = limit; 
  81.         }
  82.     
  83.                 /* calculate p */
  84.     *p = (*ptemp +(float)(excess - maxlevels *(*ptemp)) / 
  85.           (float)(maxlevels - num_above)); 
  86.     return; 
  87.     }
  88. }
  89.  
  90. calcexcess(top, histo, maxlevels)
  91.      int         top, *histo, maxlevels; 
  92. {
  93.     int         excess, 
  94.     *h; 
  95.     
  96.     excess = 0; 
  97.     for(h = histo; h < histo + maxlevels; h++)
  98.     if(*h > top)
  99.         excess += *h - top; 
  100.     return(excess); 
  101. }
  102.